#!/usr/bin/env node
import { Command } from 'commander'
import logsets from 'logsets'
import { prompt } from "enquirer"
import * as supportedFeatures from './features'
import { assignObject } from 'flex-tools'


const program = new Command()

interface FlexCIOptions{

}

program
    .name("FlexCI")
    .option("-v, --version", "当前版本号")
    .helpOption('-h, --help', '显示帮助')
    .action(async (options) => {
        const currentVersion = require("../package.json").version
        const banner = logsets.banner()
        banner.add("FlexCI CI/CD自动化工具集")
            .add("") 
            .add("Version: {}",currentVersion)
            .add("Help: {}","https://zhangfisher.github.io/flexci/")
            .render()        
        
        const featureChoices = Object.entries(supportedFeatures).map(([name,feature])=>{
            return {name:feature.title,value:name}
        })
        const questions = [{
                type   : 'multiselect',
                name   : 'selectedFeatures',
                message: '选择要启用的特性:',
                // initial: 0,
                choices: featureChoices, 
                result: function(this:any,names:string[]) {
                    return this.map(names)
                }
            }
        ]
        let { selectedFeatures } = await prompt<{selectedFeatures:string}>(questions as any);
        for(const name of Object.values(selectedFeatures)){        
            logsets.separator()
            const feature = (supportedFeatures as any)[name]
            const finalOpts = assignObject({},options,feature.options)
            await feature.install.call(feature,finalOpts,selectedFeatures)            
        }
    }) 

program.parseAsync(process.argv);